package table;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;

import utils.handlers.RowBoldWriteHandler;

class Person
{

	private Integer id;

	private String name;

	private Integer age;

	public Integer getId()
	{
		return id;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public Integer getAge()
	{
		return age;
	}

	public void setAge(Integer age)
	{
		this.age = age;
	}

}

class Fruit
{

	private Integer id;

	private String name;

	private Double price;

	public Integer getId()
	{
		return id;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public Double getPrice()
	{
		return price;
	}

	public void setPrice(Double price)
	{
		this.price = price;
	}

}

public class TebleTest
{
	private static final String write_path = "write2.xlsx";

	private static List<Person> data()
	{
		List<Person> list = new ArrayList<>();

		for (int i = 0; i < 5; i++)
		{
			Person person = new Person();
			person.setId(i);
			person.setName("瓜田李下" + i);
			person.setAge(20 + i);

			list.add(person);
		}

		return list;
	}

	private static List<Fruit> data2()
	{
		List<Fruit> list = new ArrayList<>();

		for (int i = 0; i < 5; i++)
		{
			Fruit fruit = new Fruit();
			fruit.setId(i);
			fruit.setName("apple" + i);
			fruit.setPrice((double) (4 + i));

			list.add(fruit);
		}

		return list;
	}

	public static void write()
	{
		ExcelWriter excelWriter = null;

		try
		{
			excelWriter = EasyExcel.write(write_path).build();

			WriteSheet writeSheet = EasyExcel.writerSheet().build();

			WriteTable writeTable = EasyExcel.writerTable(0).head(Person.class).needHead(false).build();
			writeTable.getCustomWriteHandlerList().add(new RowBoldWriteHandler(1));
			
			WriteTable writeTable2 = EasyExcel.writerTable(1).head(Fruit.class).needHead(false).build();
			writeTable2.getCustomWriteHandlerList().add(new RowBoldWriteHandler(2));

			excelWriter.write(data(), writeSheet, writeTable);
			excelWriter.write(data2(), writeSheet, writeTable2);
		}
		finally
		{
			if (excelWriter != null)
			{
				excelWriter.finish();
			}
		}
	}

	public static void main(String[] args)
	{
		write();
	}
}
